﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Xiaozilang_Ordering_System
{
    public partial class Business : Form
    {
        int index = 0;
        DataTable dt1 = new DataTable();
        DataTable dt2 = new DataTable();
        DataTable dt3 = new DataTable();
        public Business()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //文字背景透明
            label1.BackColor = Color.Transparent;
            label2.BackColor = Color.Transparent;
            label3.BackColor = Color.Transparent;
            label4.BackColor = Color.Transparent;
            label5.BackColor = Color.Transparent;
            label6.BackColor = Color.Transparent;
            label7.BackColor = Color.Transparent;
            label8.BackColor = Color.Transparent;

            Text = "欢迎使用小次郎";
            //行高
            dataGridView1.RowTemplate.Height = 80;
            dataGridView2.RowTemplate.Height = 60;
            //dataGridViem1标题字段居中
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView2.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView4.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            // 禁止bai用户改du变DataGridView1的所有列的列宽
            dataGridView1.AllowUserToResizeColumns = false;
            dataGridView2.AllowUserToResizeColumns = false;
            dataGridView4.AllowUserToResizeColumns = false;
            //禁止用户改变DataGridView1所有行的行高
            dataGridView1.AllowUserToResizeRows = false;
            dataGridView2.AllowUserToResizeRows = false;
            dataGridView4.AllowUserToResizeRows = false;
            //取消控件自动生成列
            //dataGridView1.AutoGenerateColumns = false;
            dataGridView2.AutoGenerateColumns = false;
            dataGridView4.AutoGenerateColumns = false;
            //取消标题行
            dataGridView1.RowHeadersVisible = false;
            dataGridView2.RowHeadersVisible = false;
            dataGridView4.RowHeadersVisible = false;
            //取消自动生成行
            dataGridView1.AllowUserToAddRows = false;
            dataGridView2.AllowUserToAddRows = false;
            dataGridView4.AllowUserToAddRows = false;

            //显示店家信息
            label8.Text = denglu.Name1;
            label7.Text = denglu.address;
            label5.Text = denglu.Money;
            //显示头像
            Pirbox(denglu.imagelj,pictureBox1);
            //搭建数据适配器，显示相关信息
            //显示菜品
                Dish_display(dataGridView1);
                //评分显示
                Rating_display();
                //显示评论
                Display_evaluation(dataGridView2);
                //显示订单
                Order_interface(dataGridView4);
        }
        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            Delete_button(e);
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GeneralVariable.count1 = 1;
            跳转小窗口();
            Dish_display(dataGridView1);
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GeneralVariable.count1 = 5;
            传输菜品数据();
            Dish_display( dataGridView1);
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GeneralVariable.count1 = 4;
            传输菜品数据();
            Dish_display( dataGridView1);
        }
        public static void Pirbox(string pir,PictureBox pictureBox) 
        {
            try
            {
                pictureBox.Image = Image.FromFile(pir);
            }
            catch (Exception ex)
            {
                MessageBox.Show("发生错误{0}", ex.Message);
            }
        }
        /*传输菜品数据
         * 无参数
         * 无返回值
         */
        private  void 传输菜品数据() 
        {
            index = dataGridView1.CurrentRow.Index;
            GeneralVariable.dishName = dataGridView1.Rows[index].Cells["dName"].Value.ToString();
            GeneralVariable.dishimage = dataGridView1.Rows[index].Cells["image"].Value.ToString();
            GeneralVariable.Price = dataGridView1.Rows[index].Cells["unitPrice"].Value.ToString();
            GeneralVariable.DishID = dataGridView1.Rows[index].Cells["dishID"].Value.ToString();
            跳转小窗口();
        }
        /*跳转小窗口
         参数：无
         返回值：无*/
        public static void 跳转小窗口() 
        {
            AddImage ad = new AddImage();
            ad.ShowDialog();
        }
        /*显示图片列方法
         * 参数：string a,string b,DataGridView dataGridView
         * 返回值：无
         */
        private void Picture_column_display(string a,string b,DataGridView dataGridView) 
        {
            try
            {
                for (int i = 0; i < dataGridView.Rows.Count; i++)
                {
                    FileStream fileStream = new FileStream(Application.StartupPath + "\\" + dataGridView.Rows[i].Cells["" + a + ""].Value.ToString(), FileMode.Open, FileAccess.Read);
                    int byteLength = (int)fileStream.Length;
                    byte[] fileBytes = new byte[byteLength];
                    fileStream.Read(fileBytes, 0, byteLength);
                    fileStream.Close();
                    dataGridView.Rows[i].Cells["" + b + ""].Value = System.Drawing.Image.FromStream(new MemoryStream(fileBytes));
                }
            }
            catch (Exception ex) 
            {
                MessageBox.Show("发生错误{0}",ex.Message);
            }
        }
        /*显示评分
         参数：无
         返回值：无*/
        private void Rating_display() 
        {
            try
            {
                string sql = "select AVG(graded) from Comment c join Business b on c.BSID=b.BSID where BSname='" + denglu.Name1 + "'";
                string s = DBHelper.ExecuteScalar(sql).ToString();
                if (s == DBNull.Value.ToString())
                {
                    label6.Text = "0.0";
                }
                else
                {
                    label6.Text = decimal.Round(decimal.Parse(s), 1).ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("404{0}", ex.Message);
            }
        }
        /*菜品显示
         * 参数：DataTable dt,DataGridView dataGridView
         * 返回值：无
         */
        private void Dish_display(DataGridView dataGridView)
        {  
            //地址列
            string a = "image";
            //图片显示列
            string b = "Column1";
            try
            {
                string sql = "select dName, unitPrice, image,dishID from Dish where BSID='" + denglu.ID + "'and condition='是'";
                dt1 = DBHelper.SelectByAdapter(sql);
                //菜品默认金额降序
                DataView view = new DataView(dt1);
                view.Sort = "unitPrice asc";
                dataGridView.DataSource = view;
            }
            catch (Exception ex) 
            {
                MessageBox.Show("发生错误{0}",ex.Message);
            }
            Picture_column_display(a, b, dataGridView1);
        }
        /*评论显示
         * 参数：DataTable dt,DataGridView dataGridView
         * 返回值：无
         */
        private void Display_evaluation(DataGridView dataGridView) 
        {
            try
            {
                string sql = "select CommentID,USimage, PLTime,USname,graded,detailedComments from Comment c join Order1 o on c.OrderID=o.OrderID join User1 u on o.userID=u.userID where c.BSID='" + denglu.ID + "'";
                dt2 = DBHelper.SelectByAdapter(sql);
                DataView view = new DataView(dt2);
                view.Sort = "PLTime desc";
                dataGridView.DataSource = view;
            }
            catch (Exception ex) 
            {
                MessageBox.Show("发生错误:{0}", ex.Message);
            }
        }
        private void tabControl1_MouseClick(object sender, MouseEventArgs e)
        {
            string a = "USimage";
            string b = "Column5";
            Picture_column_display(a, b, dataGridView2);
        }
        /*删除评论
         *参数：DataGridViewCellEventArgs e
         *返回值：无
         */
        private void Delete_button(DataGridViewCellEventArgs e) 
        {
            //删除评论功能
            try
            {
                if (dataGridView2.Columns[e.ColumnIndex].Name == "删除")
                {
                    DialogResult re = MessageBox.Show("您是否要删除该订单", "", MessageBoxButtons.OKCancel);
                    if (re == DialogResult.OK)
                    {
                        bool reader;
                        //接收控件值
                        string str = dataGridView2.Rows[e.RowIndex].Cells["CommentID"].Value.ToString();
                        string sql = "delete from Comment where CommentID='" + str + "'";
                        reader = DBHelper.GetNonQuery(sql);
                        if (reader == true)
                        {
                            MessageBox.Show("删除成功");
                            Rating_display();
                            Display_evaluation(dataGridView2);
                            string a = "USimage";
                            string b = "Column5";
                            Picture_column_display(a, b, dataGridView2);
                        }
                        else
                        {
                            MessageBox.Show("该条订单已经不存在，请刷新界面");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("发生错误:{0}", ex.Message);
            }
        }
        /*订单显示
         * 参数：
         * 返回值
         */
        private void Order_interface(DataGridView dataGridView) 
        {
            try
            {
                string sql = "select r.OrderID,USname,dName,quantity,orderTime,unitPrice, orderStatus from OrderDetail o join Order1 r on o.orderID = r.OrderID join User1 u on r.userID = u.userID join Dish d on o.dishID = d.dishID where d.BSID='" + denglu.ID + "'; ";
                dt3 = DBHelper.SelectByAdapter(sql);
                //添加列
                dt3.Columns.Add("Status", typeof(string));
                //添加金额列
                dt3.Columns.Add("Subtotal", typeof(string));
                for (int i = 0; i < dt3.Rows.Count; i++)
                {
                    decimal c = Convert.ToDecimal(dt3.Rows[i]["quantity"].ToString());
                    decimal d = Convert.ToDecimal(dt3.Rows[i]["unitprice"].ToString());
                    dt3.Rows[i]["Subtotal"] = string.Format("{0}", c * d).ToString();
                    string s = dt3.Rows[i]["orderStatus"].ToString();
                    if (s == "0")
                    {
                        dt3.Rows[i]["Status"] = "派送中";
                    }
                    else
                    {
                        dt3.Rows[i]["Status"] = "已送达";
                    }
                }
                DataView view = new DataView(dt3);
                view.Sort = "OrderID asc,orderStatus desc";
             
               
                dataGridView.DataSource =view;
            }
            catch (Exception ex) 
            {
                MessageBox.Show("发生错误{0}",ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            GeneralVariable.count1 = 2;
            跳转小窗口();
            Pirbox(denglu.imagelj, pictureBox1);
            label8.Text = denglu.Name1;
            label7.Text = denglu.address;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //刷新界面
            Business BH = new Business();
            BH.Show();
            this.Hide();
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        //搜索菜品按钮
        private void button5_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                Dish_display( dataGridView1);
            }
            else 
            {
                Dishes_inquiry();
                if (dataGridView1.Rows.Count == 0) 
                {
                    MessageBox.Show("没有相关菜品，请重新输入");
                    Dish_display( dataGridView1);
                }
            }
        }
        /*菜品模糊查询
         *返回值：无
         * 参数：无
         */
        private void Dishes_inquiry()
        {
            string a = "image";
            //图片显示列
            string b = "Column1";
            try
            {
                DataView view = new DataView(dt1);
                view.RowFilter = string.Format("dName like '%{0}%'", textBox1.Text);
                dataGridView1.DataSource = view;
            }
            catch (Exception ex) 
            {
                MessageBox.Show("发生错误{0}", ex.Message);
            }

            Picture_column_display(a,b,dataGridView1);
        }

        //搜索订单按钮
        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "")
            {
                Order_interface(dataGridView4);
            }
            else 
            {
                Order_inquiry();
                if (dataGridView4.Rows.Count == 0) 
                {
                    MessageBox.Show("没有条件匹配的订单，请重新输入");
                    Order_interface(dataGridView4);
                }
            }
        }
        /*订单模糊查询
         * 返回值：无
         * 参数：无
         */
        private void Order_inquiry() 
        {
            try
            {   
                DataView view = new DataView(dt3);
                view.RowFilter =string.Format("dName like '%{0}%'",textBox2.Text);
                dataGridView4.DataSource = view;
            }
            catch(Exception ex) 
            {
                MessageBox.Show("发生错误{0}", ex.Message);
            }
        }
    }
}

